package com.bbs.detai1713.controller;

import com.alibaba.fastjson.JSONObject;
import com.bbs.detai1713.model.*;
import com.bbs.detai1713.service.DataService;
import com.bbs.detai1713.tool.ResultInfo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

@Api(tags = "帖子版块")
@Slf4j
@RestController
public class PostingController {
    @Autowired
    DataService dataService;

    @PostMapping("/createPosting")
    public ResultInfo createPosting(@RequestBody CreatePosting createPosting) {
        log.info("创建帖子,信息为:{}", createPosting);
        return ResultInfo.success(dataService.createPosting(createPosting), "创建帖子!");
    }

    @PostMapping("/deletePosting")
    public ResultInfo deletePosting(@RequestBody DeletePosting deletePosting) {
        log.info("删除帖子,id为:{}", deletePosting);
        if (dataService.deletePosting(deletePosting) >= 1)
            return ResultInfo.success("删除帖子成功!");
        else
            return ResultInfo.failure("删除帖子失败!");
    }

    @PostMapping("/deleteFloorPosting")
    public ResultInfo deleteFloorPosting(@RequestBody DeleteFloorPosting deleteFloorPosting, @AuthenticationPrincipal UserDetails user) {
        log.info("删除回复,数据为:{}", deleteFloorPosting);
        long userId = Long.parseLong(JSONObject.parseObject(JSONObject.toJSONString(user)).get("userId").toString());
        if (dataService.deleteFloorPosting(deleteFloorPosting, userId))
            return ResultInfo.success("按楼层删除帖子成功!");
        else
            return ResultInfo.failure("按楼层删除帖子失败!");
    }

    @PostMapping("/replyPosting")
    public ResultInfo replyPosting(@RequestBody ReplyPosting replyPosting, @AuthenticationPrincipal UserDetails user) {
        log.info("回复帖子,数据为:{},userId:{}", replyPosting, Long.parseLong(JSONObject.parseObject(JSONObject.toJSONString(user)).get("userId").toString()));
        replyPosting.setAuthorId(Long.parseLong(JSONObject.parseObject(JSONObject.toJSONString(user)).get("userId").toString()));
        if (dataService.replyPosting(replyPosting) >= 1)
            return ResultInfo.success("回复帖子成功!");
        else
            return ResultInfo.failure("回复帖子失败!");
    }

    @GetMapping("/selectReply")
    public ResultInfo replyPosting(@RequestParam Integer page, @RequestParam Integer postingId) {
        log.info("查询回复,数据为:{},{}", page, postingId);

        return ResultInfo.success(dataService.selectReply((page - 1) * 5, postingId), "查询成功", dataService.countReply(postingId));
    }

    @PostMapping("/selectUserReply")
    public ResultInfo selectUserReply(@AuthenticationPrincipal UserDetails user) {
        long userId = Long.parseLong(JSONObject.parseObject(JSONObject.toJSONString(user)).get("userId").toString());
        log.info("查询我的回复,数据为,userId:{}", userId);
        return ResultInfo.success(dataService.selectUserReply(userId), "查询成功");
    }

    @PostMapping("/selectOtherReply")
    public ResultInfo selectOtherReply(@AuthenticationPrincipal UserDetails user) {
        long userId = Long.parseLong(JSONObject.parseObject(JSONObject.toJSONString(user)).get("userId").toString());
        log.info("查询别人回复我的数据,数据为,userId:{}", userId);
        return ResultInfo.success(dataService.selectOtherReply(userId), "查询成功");
    }

    @GetMapping("/selectPostingByPostingId")
    public ResultInfo selectPostingByPostingId(@RequestParam Integer postingId) {
        log.info("查询帖子,传入参数为:{}", postingId);
        return ResultInfo.success(dataService.selectPostingByPostingId(postingId), "查询帖子成功!");
    }

    @GetMapping("/selectPosting")
    public ResultInfo selectPosting(@RequestParam Integer page, @RequestParam(required = false) Integer boardId, @RequestParam(required = false) Integer userId) {
        SelectPosting selectPosting = new SelectPosting();
        int count = 0;
        if (boardId != null) {
            selectPosting.setBoardId(boardId);
            count = dataService.countPosting(boardId, 0);
        } else
            selectPosting.setBoardId(0);
        if (userId != null) {
            selectPosting.setUserId(userId);
            count = dataService.countPosting(0, userId);
        } else
            selectPosting.setUserId(0);
        selectPosting.setPage((page - 1) * 5);
        log.info("查询帖子,传入参数为:{}", selectPosting);
        return ResultInfo.success(dataService.selectPosting(selectPosting), "查询帖子成功!", count);
    }

    @PostMapping("/movePosting")
    public ResultInfo movePosting(@RequestBody MovePosting movePosting) {
        log.info("移动帖子板块,数据为:{}", movePosting);
        if (dataService.movePosting(movePosting) >= 1)
            return ResultInfo.success("移动帖子版块成功!");
        else
            return ResultInfo.success("移动帖子版块失败!");
    }

}
